package cn.lutu.domain.service.impl;

import cn.lutu.domain.repository.AdminRepository;
import cn.lutu.domain.repository.AuthorityRepository;
import cn.lutu.domain.service.AdminService;
import cn.lutu.protocol.dto.AdminSelectUserDTO;
import cn.lutu.protocol.dto.AuthorityUpdateDTO;
import cn.lutu.protocol.dto.RoleAddDTO;
import cn.lutu.protocol.dto.RoleUpdateInfoDTO;
import cn.lutu.protocol.entity.Authority;
import cn.lutu.protocol.entity.RoleAndAuthority;
import cn.lutu.protocol.entity.UserAndRole;
import cn.lutu.protocol.vo.AuthorityVO;
import cn.lutu.protocol.vo.RoleVO;
import cn.lutu.protocol.vo.adminVO.*;
import cn.lutu.util.exception.ServiceException;
import cn.lutu.util.response.StatusCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Slf4j
@Service
public class AdminServiceImpl implements AdminService {
    @Autowired
    AdminRepository adminRepository;
    @Autowired
    AuthorityRepository authorityRepository;


    @Override
    public List<AdminUserListVO> selectUserList() {
        log.info("开始处理AdminServiceUserList请求");
        return adminRepository.userList();
    }

    @Override
    public List<AdminRoleListVO> roleList() {
        log.info("开始处理AdminServiceRoleList请求");
        return adminRepository.roleList();
    }

    @Override
    public List<AdminSpotVO> spotList() {
        log.info("开始处理AdminRepositorySpotList请求");
        return adminRepository.spotList();
    }

    @Override
    public List<AuthorityUpdateVO> authorityList() {
        log.info("开始处理AuthorityUpdateVO请求");
        return adminRepository.authorityList();
    }

    public List<AdminUserListVO> userListBy(AdminSelectUserDTO adminSelectUserDTO) {
        log.info("Service层开始处理管理员根据条件查询用户信息，参数为：{}",adminSelectUserDTO);
        return adminRepository.userListBy(adminSelectUserDTO);
    }

    @Override
    public UserDetailVO getUserInfoByUsername(String userName) {
        //TODO 判断用户是否存在，需要用到UserRepository,暂无


        return adminRepository.getUserInfoByUsername(userName);
    }

    @Override
    public void deleteById(Long id) {
        log.info("开始执行根据ID删除角色的业务");
        UserAndRole userAndRole = adminRepository.selectById(id);
        if (userAndRole == null) {
            String message = "删除角色失败，尝试删除的数据不存在！";
            log.warn(message);
            throw new ServiceException(StatusCode.ERROR_NOT_FOUND, message);
        }

        int rows = adminRepository.deleteById(id);
        if (rows != 1) {
            String message = "删除角色失败，服务器忙，请稍后再试！";
            log.warn(message);
            throw new ServiceException(StatusCode.ERROR_UNKNOWN, message);
        }
    }

    @Override
    public void deleteAuthorityById(Long id) {
        log.info("开始执行根据ID删除权限的业务");

        int rows = adminRepository.deleteAuthorityById(id);
        if (rows != 1) {
            String message = "删除权限失败，服务器忙，请稍后再试！";
            log.warn(message);
            throw new ServiceException(StatusCode.ERROR_UNKNOWN, message);
        }
    }

    @Override
    public RoleUpdateInfoVO getInfoById(Long id) {
        log.debug("开始处理【根据ID查询角色】业务，参数：{}", id);
        RoleUpdateInfoVO roleUpdateInfoVO = adminRepository.getSelectById(id);
        if (roleUpdateInfoVO == null ) {
            String message = "获取标签详情失败，尝试访问的标签数据不存在！";
            log.warn(message);
            throw new ServiceException(StatusCode.ERROR_NOT_FOUND, message);
        }
        return roleUpdateInfoVO;
    }

    @Override
    public AuthorityUpdateVO getAuthority(Long id) {
        log.debug("开始处理【根据ID查询权限】业务，参数：{}", id);
        AuthorityUpdateVO authorityUpdateVO = adminRepository.getAuthorityById(id);
        if (authorityUpdateVO == null ) {
            String message = "获取标签详情失败，尝试访问的标签数据不存在！";
            log.warn(message);
            throw new ServiceException(StatusCode.ERROR_NOT_FOUND, message);
        }
        return authorityUpdateVO;
    }

    @Override
    public AdminSpotVO getSpotById(Long id) {
        log.debug("开始处理【根据ID查询景点】业务，参数：{}", id);
        AdminSpotVO adminSpotVO = adminRepository.getSpotById(id);
        if (adminSpotVO == null ) {
            String message = "获取标签详情失败，尝试访问的标签数据不存在！";
            log.warn(message);
            throw new ServiceException(StatusCode.ERROR_NOT_FOUND, message);
        }
        return adminSpotVO;
    }

    @Override
    public void addNewRole(RoleAddDTO roleAddDTO) {
        log.info("开始执行新增角色的功能，名称为：{}",roleAddDTO);
        if (roleAddDTO == null) {
            String message = "您输入的数据为空!";
            log.debug(message);
            throw new ServiceException(StatusCode.ERROR_NOT_FOUND,message);
        }
        RoleVO roleVO = adminRepository.getRoleInfoByName(roleAddDTO.getName());
        if(roleVO != null){
            String message = "角色名已存在!";
            log.debug(message);
            throw new ServiceException(StatusCode.USERNAME_ALREADY_EXISTS,message);
        }
        UserAndRole userAndRole =new UserAndRole();
        BeanUtils.copyProperties(roleAddDTO,userAndRole);
        userAndRole.setSort(2);
        adminRepository.insert(userAndRole);
    }

    @Override
    public void addNewAuthority(Long roleId,Long authorityId) {
        log.info("开始执行新增权限的功能");
        RoleAndAuthority roleAndAuthority=new RoleAndAuthority();
        roleAndAuthority.setAuthorityId(authorityId);
        roleAndAuthority.setRoleId(roleId);
        authorityRepository.insert(roleAndAuthority);
    }

    @Override
    public List<AuthorityVO> authList() {
        return authorityRepository.selectAll();
    }

    @Override
    public void updateInfoById(RoleUpdateInfoDTO roleUpdateInfoDTO) {
        log.debug("开始处理【修改用户角色】的业务，参数：{}", roleUpdateInfoDTO);

        Long id = roleUpdateInfoDTO.getId();
        RoleUpdateInfoVO roleUpdateInfoVO = adminRepository.getSelectById(id);
        if (roleUpdateInfoVO == null) {
            String message = "修改角色失败，尝试修改的角色数据不存在！";
            log.warn(message);
            throw new ServiceException(StatusCode.ERROR_NOT_FOUND, message);
        }

        String name = roleUpdateInfoDTO.getName();
        int count = adminRepository.countByNameAndNotId(id, name);
        if (count > 0) {
            String message = "修改角色失败，角色名称已经被占用！";
            log.warn(message);
            throw new ServiceException(StatusCode.ERROR_UPDATE, message);
        }

        UserAndRole userAndRole = new UserAndRole();
        BeanUtils.copyProperties(roleUpdateInfoDTO, userAndRole);
        int rows = adminRepository.updateById(userAndRole);
        if (rows != 1) {
            String message = "修改角色失败，服务器忙，请稍后再试！";
            log.warn(message);
            throw new ServiceException(StatusCode.OPERATION_FAILED, message);
        }
    }

    @Override
    public void updateAuthorityById(AuthorityUpdateDTO authorityUpdateDTO) {
        log.debug("开始处理【修改用户权限】的业务，参数：{}", authorityUpdateDTO);

        Long id = authorityUpdateDTO.getId();
        AuthorityUpdateVO authorityUpdateVO = adminRepository.getAuthorityById(id);
        if (authorityUpdateVO == null) {
            String message = "修改权限失败，尝试修改的权限数据不存在！";
            log.warn(message);
            throw new ServiceException(StatusCode.ERROR_NOT_FOUND, message);
        }

        String name = authorityUpdateVO.getName();
        int count = adminRepository.countByNameAndNotId(id, name);
        if (count > 0) {
            String message = "修改权限失败，权限名称已经被占用！";
            log.warn(message);
            throw new ServiceException(StatusCode.ERROR_UPDATE, message);
        }

        Authority authority = new Authority();
        BeanUtils.copyProperties(authorityUpdateDTO, authority);
        int rows = adminRepository.updateAuthorityById(authority);
        if (rows != 1) {
            String message = "修改权限失败，服务器忙，请稍后再试！";
            log.warn(message);
            throw new ServiceException(StatusCode.OPERATION_FAILED, message);
        }

    }
}
